﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    private List<DateTime> EventDates;
    private List<DateTime> EntryDates;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lbUsername.Text = Session["Username"].ToString();
            int diaryID = Convert.ToInt32(Session["DiaryID"]);
            EventDates = GetEventDates(diaryID);
            Session["EventDates"] = EventDates;
            EntryDates = GetDiaryEntryDates(diaryID);
            Session["EntryDates"] = EntryDates;
        }
    }

    private List<DateTime> GetEventDates(int diaryID)
    {
        List<DateTime> eventDates = new List<DateTime>();
        string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);
        conn.Open();
        string sql = "Select EventDate From DiaryEvent Where DiaryID = @1";
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = sql;
        cmd.Parameters.AddWithValue("@1", diaryID);
        SqlDataReader reader = cmd.ExecuteReader();
        if (reader.HasRows)
        {
            while (reader.Read())
            { 
                eventDates.Add(reader.GetDateTime(0));
            }
            reader.Close();
        }
        cmd.Dispose();
        conn.Close();
        return eventDates;
    }

    private List<DateTime> GetDiaryEntryDates(int diaryID)
    {
        List<DateTime> entryDates = new List<DateTime>();
        string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);
        conn.Open();
        string sql = "Select EntryDate From DiaryEntry Where DiaryID = @1";
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = sql;
        cmd.Parameters.AddWithValue("@1", diaryID);
        SqlDataReader reader = cmd.ExecuteReader();
        if (reader.HasRows)
        {
            while (reader.Read())
            { 
                entryDates.Add(reader.GetDateTime(0));
            }
            reader.Close();
        }
        cmd.Dispose();
        conn.Close();
        return entryDates;
    }

    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        EventDates = Session["EventDates"] as List<DateTime>;
        foreach (var date in EventDates)
        {
            if (e.Day.Date == date.Date)
                e.Cell.ForeColor = Color.Red;
        }
        EntryDates = Session["EntryDates"] as List<DateTime>;
        foreach (var date in EntryDates)
        {
            if (e.Day.Date == date.Date)
                e.Cell.BackColor = Color.Blue;
        }
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        Session["SelectedDate"] = Calendar1.SelectedDate;        
        Response.Redirect("NewEntry.aspx");
    }
}
